<?php

/*

  Action	$3

  +$2
  Reveal the top 3 cards of the Black Market deck. You may buy one of them immediately.
  Put the unbought cards on the bottom of the Black Market deck in any order.

  Setup: make a Black Market deck out of one copy of each Kingdom card not in the supply.

 */

class BlackMarket extends Card implements iAction, iKingdom, iPromotion {

    public function __construct() {
        parent::__construct("Black Market", 3);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }
    
    public function isImplemented() {
        return false;
    }

    public function getDescription() {
        return array(
            "+2$",
            "Reveal the top 3 cards of the Black Market deck. You may buy one of them immediately. Put the unbought cards on the bottom of the Black Market deck in any order.",
            "Setup: make a Black Market deck out of one copy of each Kingdom card not in the supply.");
    }

    public function play(Game $game, $isRealCard) {

        $game->addMoney(new Price(2));
        $cards = $game->getTopBlackMarketCards(3);
        ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($cards), "reveals " . ScreenDisplay::$LMSG);


        if (sizeof($cards) > 0) {
            $options = array();
            foreach ($cards as $card) {
                $options[] = $card;
            }
            $options[] = new Option(1, 'None');
            
            $output = MySocket::GetUserReplyBuyBeforeAction("BlackMarket", $game, Input_ChooseOne, $options, 1, 1, null, "Choose card to buy", null, true);
        } else {
            $game->cardActionFinished();
        }
    }

    public function buyCard(Game $game, $card_name) {

        $cards = array();
        if ($card_name == 'None') {
            ScreenDisplay::addScreenMessage($game, true, "buy nothing", "buys nothing");
            $cards = $game->removeTopBlackMarketCards(3);
        } else {
            $newcard = $game->buyCardFromBlackMarket($card_name);
            if ($newcard) {
                ScreenDisplay::addScreenMessage($game, true, "buy " . nicePrint($newcard), "buys " . ScreenDisplay::$LMSG);
                $cards = $game->removeTopBlackMarketCards(2);
            } else {
                #if the user can't buy a card because it is too pricy, let him know, and present the options again
                ScreenDisplay::addScreenMessage($game, true, "can't buy " . nicePrint($card_name) . " - not enough money", "can't buy " . nicePrint($card_name) . " - not enough money");
                $bm_cards = $game->getTopBlackMarketCards(3);
                if (sizeof($bm_cards) > 0) {
                    $options = array();
                    foreach ($bm_cards as $bm_card) {
                        $options[] = $bm_card;
                    }
                    $options[] = new Option(1, 'None');
                    $output = MySocket::GetUserReplyBuyBeforeAction("BlackMarket", $game, Input_ChooseOne, $options, 1, 1, null, "Choose card to buy", null, true);
                    return;
                }
            }
        }

        $output = MySocket::GetUserReply($game, Input_Reorder, $cards, -1, -1, null, "Choose card order to put at the bottom of black market deck");
        ScreenDisplay::addScreenMessage($game, true, "put " . nicePrint($output) . " on the bottom of black market pile", "puts " .ScreenDisplay::$LMSG . " on the bottom of black market pile");
        foreach ($output as $p) {
            $game->putCardOnBottomOfBlackMarket($p);
        }

        $game->cardActionFinished();
        return true;
    }

    public function boardSetup(Game $game) {
        $game->addBlackMarket();
        return true;
    }

}

?>